package com.kaka.bus.engine.home.info.amountlist;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.OrderBean;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.common.impl.LoanService;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

public class AmountPresenter extends RxPresenter<AmountContract.View> implements AmountContract.Presenter {

    private LoanService mLoanService;

    @Inject
    public AmountPresenter(DataManager dataManager, LoanService loanService) {
        super(dataManager);
        mLoanService = loanService;
        setService(mLoanService);
    }



    public void getAmountList(String userId, String token, int pageNum, int pageSize,String device,String version) {
        request(userId, token, pageNum, pageSize, false,device,version);
    }

    @Override
    public void loadMore(String userId, String token, int pageNum, int pageSize,String device,String version) {
        request(userId, token, pageNum, pageSize, true,device,version);
    }

    @Override
    public void cancelRequest(String userId, String token, String borr_id) {

    }

    private void request(String userId, String token, int pageNum, int pageSize, boolean isLoadMore, String device, String version) {
        addSubscribe(mDataManager.getApi()
                .getAmountList(userId, token, pageNum, pageSize, device, version)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<OrderBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<OrderBean>> data) {
                        if (isLoadMore) {
                            mView.onResultLoadMore(data);
                        } else {
                            mView.onResultStart(data);
                        }
                    }

                    @Override
                    protected void onFaild() {
                        if (!isLoadMore)
                            mView.onError();

                    }
                }));
    }

}
